# encoding: utf-8
# language: zh-CN

def step_pause
  sleep(STEP_PAUSE)
end

def is_view_exist view
  not is_view_not_exist view
end

def is_view_not_exist view
  query("view marked:'#{view}'").empty?
end

def is_not_navigation title
  query("navigationBar id:'#{title}'").empty?
end

def is_exists_tableViewCell
  query("tableViewCell").count > 0
end

def wait_view_to_display text
  wait_for(WAIT_TIMEOUT) {
    query("view marked:'#{text}'").size > 0
  }
end

def click_anywhere
  touch(nil, {:offset => {:x => 50, :y => 50}})
  sleep(STEP_PAUSE)
end

def touch_view(text, view_index="1")
  wait_view_to_display text
  index = view_index.to_i - 1
  touch("view marked:'#{text}' index:#{index}")
  sleep(STEP_PAUSE)
end

def touch_view_if_exist text
  if is_view_exist text then
    touch_view text
  end
end

def touch_alert_button(name)
  touch("alertButton marked:'#{name}'")
  sleep(STEP_PAUSE)
end

def touch_hold_view view_id
  touch_hold("view marked:'#{view_id}'")
  sleep(STEP_PAUSE)
end

def touch_button(button_name)
  wait_view_to_display button_name
  touch("* marked:'#{button_name}'")
end

def touch_tab_bar_button button_name
  touch("tabBarButton marked:'#{button_name}'")
  sleep(STEP_PAUSE)
end

def touch_table_view_cell
  touch("tableViewCell index:0")
end

def clear_text_field field_name
  wait_view_to_display field_name
  clear_text("textField accessibilityLabel:'#{field_name}'")
  sleep(STEP_PAUSE)
end

def clear_textView field_name
  wait_view_to_display field_name
  clear_text("textView accessibilityLabel:'#{field_name}'")
  sleep(STEP_PAUSE)
end

def type_text(field_name, text)
    wait_view_to_display field_name
    touch_view field_name
    type_text_in "textField marked:'#{field_name}'", text
end

def type_text_in_first_field(text)
    type_text_in "textField index:0", text
    sleep(STEP_PAUSE)
end

def type_text_in query, text
    max_step = 20
    touch query

    while not keyboard_visible?
      if max_step <= 0 then
        break
      end
      touch query
      max_step -= 1
      sleep(STEP_PAUSE)
    end

    wait_for_keyboard()
    keyboard_enter_text text
    uia_enter
    sleep(STEP_PAUSE)
end

def type_textView(field_name, text)
    wait_view_to_display field_name
    touch("textView marked:'#{field_name}'")
    wait_for_keyboard()
    keyboard_enter_text text
    uia_enter
    sleep(STEP_PAUSE)
end

def there_is(expected_mark)
    wait_for(WAIT_TIMEOUT) { view_with_mark_exists( expected_mark ) }
end

def there_are(expected_mark)
    wait_for(WAIT_TIMEOUT) {
      count = query("view {text LIKE '*#{expected_mark}*'}").count
      return count - 1
    }
end

def assert_alert_message text
  if uia_query(:alert, marked:text).empty? && (is_view_not_exist text) then
    screenshot_and_raise "Alert with message '#{text}' not found."
  end
end

def keyboard_enter_text_is text
    if query("expandingTextView {description LIKE '*\\'#{text}\\'*'}").empty?
        screenshot_and_raise "Expect keyboard has type '#{text}'"
    end
end

def scroll_default_view direction
    directions = { '上' => 'up', '下' => 'down', '左' => 'left', '右' => 'right' }
    scroll("scrollView", directions[direction])
    sleep(STEP_PAUSE)
end

def scroll_view view_id,direction
    directions = { '上' => 'up', '下' => 'down', '左' => 'right', '右' => 'left' }
    scroll("scrollView marked:'#{view_id}' index:0", directions[direction])
    sleep(STEP_PAUSE)
end

def scroll_down_if_view_not_exist view
  max_step = 20
  while is_view_not_exist (view)
    if max_step <= 0 then
      break
    end
    scroll_default_view "下"
    max_step -= 1
  end
end

def scroll_to_row_of_cell(tableView, row)
  wait_view_to_display tableView
  scroll_to_cell(:row =>row,:section => 0)
end

def assert_view_exist text
  wait_for_text(text, timeout: 10)
end

def wait_for_any_button
  wait_for(WAIT_TIMEOUT) {
    not query("button").empty?
  }
end

def wait_for_dismiss text
  wait_for(WAIT_TIMEOUT) {
    query("* {description LIKE[c] '*#{text}*'}").empty?
  }
end

def pan_view(from,to)
  wait_for(WAIT_TIMEOUT) {
    pan("view marked:'#{from}'","view marked:'#{to}'")
  }
end

def get_view_text text
  view_text = query("view marked:'#{text}'")[0]["text"]
  return view_text
end
